/**
 * @Description: 全局消息提示组件
 * 在Message组件的原型上添加方法，来不断复用同一个message实例
 * @author zhaojiuyi
 * @date 2023/4/15
 */
import {message} from "antd";
import {MessageInstance} from "antd/es/message/interface";

/* message实例 */
let messageInstance: MessageInstance

export function MyMessage() {
    const [messageApi, contextHolder] = message.useMessage();
    messageInstance = messageApi

    return (
        <>
            {contextHolder}
        </>
    )

}

/* 消息提示的方法添加到这里 */
let messageOption: { [key: string]: Function } = {
    /* 错误消息提示 */
    errorMessage: (msg: string) => {
        messageInstance.error(msg)
    },
    /* 成功消息提示 */
    successMessage: (msg: string) => {
        messageInstance.success(msg)
    },
    /*  */
    warnMessage:(msg:string) => {
        messageInstance.warning(msg)
    }
}

/* 将提示方法添加到原型对象上 */
for (let messageKey in messageOption) {
    Object.defineProperty(MyMessage.prototype, messageKey, {
        value: messageOption[messageKey]
    })
}

export const successMessage = MyMessage.prototype.successMessage
export const errorMessage = MyMessage.prototype.errorMessage
export const warnMessage = MyMessage.prototype.warnMessage


